package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.PersonTeachingHistoryMapper;
import com.zhijian.medical.entity.po.PersonTeachingHistory;
import com.zhijian.medical.entity.vo.request.PersonTeachingHistorySearchVo;
import com.zhijian.medical.entity.vo.response.PersonTeachingHistoryVo;
import com.zhijian.medical.service.PersonTeachingHistoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-带教记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-08-12 18:09:58
 */
@Slf4j
@Service
public class PersonTeachingHistoryServiceImpl implements PersonTeachingHistoryService {

    @Autowired
    private PersonTeachingHistoryMapper mapper;

    @Override
    public PersonTeachingHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonTeachingHistoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonTeachingHistory personTeachingHistory) {
        Date now = new Date();
        personTeachingHistory.setCreateTime(now);
        personTeachingHistory.setUpdateTime(now);
        return mapper.insert(personTeachingHistory) > 0 ? personTeachingHistory.getId() : 0L;
    }

    @Override
    public boolean update(PersonTeachingHistory personTeachingHistory) {
        Date now = new Date();
        personTeachingHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personTeachingHistory) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonTeachingHistory record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonTeachingHistorySearchVo personTeachingHistorySearchVo) {
        return mapper.count(personTeachingHistorySearchVo);
    }

    @Override
    public List<PersonTeachingHistoryVo> selectList(PersonTeachingHistorySearchVo personTeachingHistorySearchVo) {
        return mapper.selectList(personTeachingHistorySearchVo);
    }

    @Override
    public Page<PersonTeachingHistoryVo> selectPage(PersonTeachingHistorySearchVo personTeachingHistorySearchVo) {
        //先直接查总数
        int count = count(personTeachingHistorySearchVo);
        //分页
        Page<PersonTeachingHistoryVo> page = new Page<>(true, count, personTeachingHistorySearchVo.getPageNum(), personTeachingHistorySearchVo.getPageSize());
        personTeachingHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonTeachingHistoryVo> responseVos = this.selectList(personTeachingHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PersonTeachingHistory> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}